/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao.util;

import model.pojo.KhachHang;
import org.hibernate.HibernateException;
import org.hibernate.Session;

/**
 *
 * @author LaiHuynh
 */
public class KhachHangDAO {

    public static KhachHang dangNhap(String tenDangNhap, String matKhau) {
        KhachHang kh = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select kh from KhachHang kh where kh.tenDangNhap =:tenDangNhap and kh.matKhau =:matKhau";
            org.hibernate.Query query = session.createQuery(hql);
            query.setString("tenDangNhap", tenDangNhap);
            query.setString("matKhau", matKhau);
            kh = (KhachHang) query.uniqueResult();
        } catch (HibernateException ex) {
            //Log the exception 
            System.err.println(ex);
        } finally {
            session.close();
        }
        return kh;
    }

    public static void dangKy(KhachHang kh) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(kh);
        session.getTransaction().commit();
        session.close();
    }
}
